﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SenerPro;

namespace MensUnoShop.Models
{
    class DBTag
    {
        private DataClassesMenDataContext _dc;
        private Logger _log = new Logger(Config.logpath);

        
        public Tag GetTag(long tagid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tt = _dc.V_TagList.FirstOrDefault(t=>t.TAGid==tagid);
                    if (tt != null)
                    {
                        Tag t = new Tag() 
                                { 
                                    ALIAS = tt.TUAalias, 
                                    ID = tt.TAGid,
                                    LV = tt.TAGlevel.HasValue?tt.TAGlevel.Value:0,
                                    TITLE = tt.TAGtitle,
                                    ALT=tt.TAGalt, 
                                    PARENTID = tt.TAGparentid.HasValue?tt.TAGparentid.Value:0, 
                                    URI = tt.TUAurl, 
                                    TAGTYPE = (Config.TagType)tt.TAGtype
                                };
                        t.GetUri();
                        return t;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetTag", ex.ToString());
            }

            return null;
        }

        public Tag GetTag(string alias)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tt = _dc.V_TagList.FirstOrDefault(t => t.TUAalias == alias);
                    if (tt != null)
                    {
                        Tag t = new Tag()
                        {
                            ALIAS = tt.TUAalias,
                            ID = tt.TAGid,
                            LV = tt.TAGlevel.HasValue ? tt.TAGlevel.Value : 0,
                            TITLE = tt.TAGtitle,
                            ALT = tt.TAGalt,
                            PARENTID = tt.TAGparentid.HasValue ? tt.TAGparentid.Value : 0,
                            URI = tt.TUAurl,
                            TAGTYPE = (Config.TagType)tt.TAGtype
                        };
                        t.GetUri();
                        return t;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetTag", ex.ToString());
            }

            return null;
        }

        public List<Tag> GetClassTagList(long parentid)
        {
            List<Tag> tlist = new List<Tag>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var taglist = _dc.V_TagList.Where(t=>t.TAGtype==(int)Config.TagType.CLASS && t.TAGparentid==parentid);
                    if (taglist != null)
                    {
                        foreach (var tag in taglist)
                        {
                            Tag t = new Tag() { ALIAS = tag.TUAalias, ID = tag.TAGid, LV = tag.TAGlevel.Value, TITLE = tag.TAGtitle, ALT = tag.TAGalt, PARENTID = tag.TAGparentid.Value, URI = tag.TUAurl, TAGTYPE = (Config.TagType)tag.TAGtype };
                            t.GetUri();
                            tlist.Add(t);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetClassTagList", ex.ToString());
            }

            return tlist;
        }
        public List<Tag> GetFreeTagList(long parentid)
        {
            List<Tag> tlist = new List<Tag>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tr = _dc.UTagRelation.Where(t => t.TRclasstagid == parentid).Select(t => t.TRfreetagid);
                    if (tr != null)
                    {
                        var taglist = _dc.V_TagList.Where(t => t.TAGtype == (int)Config.TagType.FREE && tr.Contains(t.TAGid));
                        if (taglist != null)
                        {
                            foreach (var tag in taglist)
                            {
                                Tag t = new Tag() { ID = tag.TAGid, TITLE = tag.TAGtitle, ALT = tag.TAGtitle, PARENTID = parentid, TAGTYPE = (Config.TagType)tag.TAGtype };
                                t.GetUri();
                                tlist.Add(t);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetFreeTagList", ex.ToString());
            }

            return tlist;
        }

        public List<Tag> GetEventNavTagList(long tagid, Guid mkguid)
        {
            List<Tag> tlist = new List<Tag>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    //取該商品的分類
                    var tid = _dc.UTagMarket.FirstOrDefault(t => t.TMmkguid == mkguid && t.UTag.TAGtype == (int)Config.TagType.CLASS);
                    if (tid != null)
                    {
                        //取分類下的活動標籤
                        var tr = _dc.UTagRelation.FirstOrDefault(t => t.TRfreetagid == tagid && t.TRclasstagid == tid.UTag.TAGparentid);
                        if (tr != null)
                        {
                            var classt = _dc.V_TagList.SingleOrDefault(t => t.TAGid==tr.TRclasstagid);
                            if (classt != null)
                            {
                                Tag t = new Tag()
                                {
                                    ID = classt.TAGid,
                                    TITLE = classt.TAGtitle,
                                    ALT = classt.TAGtitle,
                                    PARENTID = classt.TAGparentid.Value,
                                    TAGTYPE = (Config.TagType)classt.TAGtype,
                                    LV = classt.TAGlevel.Value,
                                    ALIAS = classt.TUAalias
                                };
                                t.GetUri();
                                tlist.Add(t);
                            }
                            var eventt = _dc.V_TagList.SingleOrDefault(t => t.TAGid == tr.TRfreetagid);
                            if (eventt != null)
                            {
                                Tag t = new Tag() { ID = eventt.TAGid, TITLE = eventt.TAGtitle, ALT = eventt.TAGtitle, TAGTYPE = (Config.TagType)eventt.TAGtype };
                                t.PARENTID = tr.TRclasstagid;
                                t.GetUri();
                                tlist.Add(t);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetEventNavTagList", ex.ToString());
            }

            return tlist;
        }

        public List<Tag> GetComboTagList(long parentid)
        {
            List<Tag> tlist = new List<Tag>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tr = _dc.UTagRelation.Where(t => t.TRclasstagid == parentid).Select(t => t.TRfreetagid);
                    if (tr != null)
                    {
                        var taglist = _dc.V_TagList.Where(t => t.TAGtype == (int)Config.TagType.COMBO && tr.Contains(t.TAGid));
                        if (taglist != null)
                        {
                            foreach (var tag in taglist)
                            {
                                if (_dc.USalesKit.Any(s => s.SStagid == tag.TAGid.ToString() && s.SSstatus && s.SSstartdate <= DateTime.Now && s.SSenddate >= DateTime.Now && s.SSkind == (int)Config.SalesKitKind.ComboDiscount))
                                {
                                    Tag t = new Tag() { ID = tag.TAGid, TITLE = tag.TAGtitle, ALT = tag.TAGtitle, PARENTID = parentid, TAGTYPE = (Config.TagType)tag.TAGtype };
                                    t.GetUri();
                                    tlist.Add(t);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetComboTagList", ex.ToString());
            }

            return tlist;
        }

        public List<Tag> GetClassTagList(long parentid,int amount)
        {
            List<Tag> tlist = new List<Tag>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var taglist = _dc.V_TagList.Where(t => t.TAGtype == (int)Config.TagType.CLASS && t.TAGparentid == parentid).Take(amount);
                    if (taglist != null)
                    {
                        foreach (var tag in taglist)
                        {
                            Tag t = new Tag() { ALIAS = tag.TUAalias, ID = tag.TAGid, LV = tag.TAGlevel.Value, TITLE = tag.TAGtitle, ALT = tag.TAGalt, PARENTID = tag.TAGparentid.Value, TAGTYPE = (Config.TagType)tag.TAGtype };
                            t.GetUri();
                            tlist.Add(t);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetClassTagList", ex.ToString());
            }

            return tlist;
        }

        public List<Tag> GetNavTagList(long tagid)
        {
            List<Tag> tlist = new List<Tag>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var taglist = _dc.SP_GetNavTag(tagid);
                    if (taglist != null)
                    {
                        foreach (var tag in taglist)
                        {
                            Tag t = new Tag() { ALIAS = tag.alias, ID = tag.id.Value, LV = tag.lv.Value, TITLE = tag.title, ALT = tag.alt, PARENTID = tag.parentid.Value, TAGTYPE = Config.TagType.CLASS };
                            t.GetUri();
                            tlist.Add(t);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetNavTagList", ex.ToString());
            }

            return tlist;
        }

        public long CheckTag(string tagtitle, int tagtype)
        {
            long tagid = 0;
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var t = _dc.UTag.FirstOrDefault(tt => tt.TAGtitle == tagtitle && tt.TAGtype == tagtype);
                    if (t == null)
                    {
                        t = new UTag();
                        t.TAGdatein = DateTime.Now;
                        t.TAGstatus = true;
                        t.TAGtitle = tagtitle;
                        t.TAGtype = tagtype;
                        _dc.UTag.InsertOnSubmit(t);
                        _dc.SubmitChanges();
                        tagid = t.TAGid;
                    }
                    else
                        tagid = t.TAGid;
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "CheckTag", ex.ToString());
            }
            return tagid;
        }

        public long CheckTag(string tagtitle, int tagpareid, int tagtype)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var t = _dc.UTag.FirstOrDefault(tt => tt.TAGparentid == tagpareid && tt.TAGtype == tagtype);
                    if (t == null)
                    {
                        t = new UTag();
                        t.TAGdatein = DateTime.Now;
                        t.TAGstatus = true;
                        t.TAGtitle = tagtitle;
                        t.TAGtype = tagtype;
                        t.TAGparentid = tagpareid;
                        _dc.UTag.InsertOnSubmit(t);
                        _dc.SubmitChanges();
                        return t.TAGid;
                    }
                    else
                        return t.TAGid;
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "CheckTagPDSP", ex.ToString());
            }
            return 0;
        }

        public List<Tag> GetTagList(Config.TagType tagtype, long taglevel)
        {
            List<Tag> tlist = new List<Tag>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var taglist = _dc.V_TagList.Where(t => t.TAGtype == (int)tagtype && t.TAGlevel == taglevel);
                    if (taglist != null)
                    {
                        foreach (var tag in taglist)
                        {
                            Tag t = new Tag() { ALIAS = tag.TUAalias, ID = tag.TAGid, LV = tag.TAGlevel.Value, TITLE = tag.TAGtitle, ALT = tag.TAGalt, PARENTID = tag.TAGparentid.Value, URI = tag.TUAurl, TAGTYPE = (Config.TagType)tag.TAGtype };
                            t.GetUri();
                            tlist.Add(t);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetTagList", ex.ToString());
            }

            return tlist;
        }

        public UTagMarket GetTagMarket(Guid mkgid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var utm = _dc.UTagMarket.FirstOrDefault(tm => tm.TMmkguid == mkgid);
                    if (utm != null)
                    {
                        UTagMarket tm = new UTagMarket() { TMid = utm.TMid, TMmkguid = utm.TMmkguid, TMtagid = utm.TMtagid, TMsequence = utm.TMsequence, TMstatus = utm.TMstatus };
                        return tm;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetTagMarket", ex.ToString());
            }

            return null;
        }

        public List<Tag> GetTagList(Config.TagType tagtype)
        {
            List<Tag> tlist = new List<Tag>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var taglist = _dc.V_TagList.Where(t => t.TAGtype == (int)tagtype);
                    if (taglist != null)
                    {
                        foreach (var tag in taglist)
                        {
                            Tag t = new Tag() { ALIAS = tag.TUAalias, ID = tag.TAGid, TITLE = tag.TAGtitle, ALT = tag.TAGalt, TAGTYPE = (Config.TagType)tag.TAGtype };
                            //t.GetUri();
                            tlist.Add(t);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetTagList", ex.ToString());
            }

            return tlist;
        }

        public Tag GetTagOnly(long tagid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tt = _dc.V_TagList.FirstOrDefault(t => t.TAGid == tagid);
                    if (tt != null)
                    {
                        Tag t = new Tag() { ALIAS = tt.TUAalias, ID = tt.TAGid, TITLE = tt.TAGtitle, ALT = tt.TAGalt, TAGTYPE = (Config.TagType)tt.TAGtype };
                        t.GetUri();
                        return t;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetTagOnly", ex.ToString());
            }

            return null;
        }


        public List<UTag> GetManagerTagList(Config.TagType tagType)
        {
            List<UTag> utlist = new List<UTag>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tt = _dc.UTag.Where(t => t.TAGtype==(int)tagType && t.TAGtitle!="N/A");
                    if (tt != null)
                        utlist = tt.ToList();
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetManagerTagList", ex.ToString());
            }

            return utlist;
        }

        public bool AddManagerTag(string title, Config.TagType tagType)
        {
            bool isadd = false;
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var t = _dc.UTag.FirstOrDefault(tt => tt.TAGtitle == title);
                    if (t == null)
                    {
                        UTag ut = new UTag();
                        ut.TAGalt = string.Empty;
                        ut.TAGstatus = true;
                        ut.TAGtitle = title;
                        ut.TAGtype = (int)tagType;
                        ut.TAGdatein = DateTime.Now;
                        _dc.UTag.InsertOnSubmit(ut);
                        _dc.SubmitChanges();
                        isadd = true;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "AddManagerTag", ex.ToString());
            }

            return isadd;
        }

        public void ChgManagerTagStatus(int tid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tt = _dc.UTag.SingleOrDefault(t => t.TAGid == tid);
                    if (tt != null)
                    {
                        tt.TAGstatus = !tt.TAGstatus;
                        _dc.SubmitChanges();
                    }
                    var tm = _dc.UTagMarket.Where(t => t.TMtagid == tid);
                    foreach(var t in tm)
                    {
                        t.TMstatus = tt.TAGstatus;
                        _dc.SubmitChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "ChgManagerTagStatus", ex.ToString());
            }
        }

        public void DelManagerTag(int tid)
        {
            List<UTag> utlist = new List<UTag>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tt = _dc.UTag.SingleOrDefault(t => t.TAGid == tid);
                    if (tt != null)
                    {
                        _dc.UTagMarket.DeleteAllOnSubmit(tt.UTagMarket);
                        _dc.UTagUrlAlias.DeleteAllOnSubmit(tt.UTagUrlAlias);
                        _dc.UTag.DeleteOnSubmit(tt);
                        _dc.SubmitChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "DelManagerTag", ex.ToString());
            }
        }

        public Dictionary<UTag,UTagUrlAlias> GetManagerTagList(int level, int parent, Config.TagType tagType)
        {
            Dictionary<UTag, UTagUrlAlias> utlist = new Dictionary<UTag,UTagUrlAlias>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tt = _dc.UTag.Where(t => t.TAGtype == (int)tagType && t.TAGlevel==level);
                    
                    if (tt != null)
                    {
                        if (level > 1)
                            tt = tt.Where(t => t.TAGparentid == parent);
                        foreach(var t in tt)
                        {
                            utlist.Add(t,t.UTagUrlAlias.SingleOrDefault());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetManagerTagList", ex.ToString());
            }

            return utlist;
        }

        public Dictionary<UTag, UTagUrlAlias> GetManagerTag(int tid)
        {
            Dictionary<UTag, UTagUrlAlias> utlist = new Dictionary<UTag, UTagUrlAlias>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tt = _dc.UTag.SingleOrDefault(t => t.TAGid==tid);
                    if (tt != null)
                    {
                        utlist.Add(tt, tt.UTagUrlAlias.SingleOrDefault());
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetManagerTag", ex.ToString());
            }

            return utlist;
        }

        public UTag GetManagerTag(int tid, Config.TagType ctype)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tt = _dc.UTag.SingleOrDefault(t => t.TAGid == tid && t.TAGtype==(int)ctype);
                    if (tt != null)
                    {
                        return tt;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetManagerTag", ex.ToString());
            }

            return null;
        }

        public bool UptManagerTag(int tid, string title, string alias, string tagalt, string url)
        {
            bool isupt = false;
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var t = _dc.UTag.SingleOrDefault(tt => tt.TAGid==tid);
                    if (t != null)
                    {
                        t.TAGalt = tagalt??"";
                        t.TAGtitle = title??"";
                        var at = t.UTagUrlAlias.SingleOrDefault();
                        if (at != null)
                        {
                            at.TUAalias = alias ?? "";
                            at.TUAurl = url ?? "";
                        }
                        _dc.SubmitChanges();
                        isupt = true;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "UptManagerClassTag", ex.ToString());
            }

            return isupt;
        }

        public bool AddManagerClassTag(string title, string alias, string tagalt, string url, int level, int parentid)
        {
            bool isadd = false;
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var t = _dc.UTag.FirstOrDefault(tt => tt.TAGtitle == title);
                    if (t == null && !string.IsNullOrEmpty(title))
                    {
                        t = new UTag();
                        t.TAGalt = tagalt ?? "";
                        t.TAGtitle = title ?? "";
                        t.TAGstatus = true;
                        t.TAGtype = (int)Config.TagType.CLASS;
                        t.TAGdatein = DateTime.Now;
                        t.TAGlevel = level;
                        t.TAGparentid = parentid;
                        _dc.UTag.InsertOnSubmit(t);
                        _dc.SubmitChanges();
                        UTagUrlAlias at = new UTagUrlAlias();
                        at.TUAalias = alias ?? "";
                        at.TUAtagid = t.TAGid;
                        at.TUAurl = url ?? "";
                        _dc.UTagUrlAlias.InsertOnSubmit(at);
                        _dc.SubmitChanges();
                        isadd = true;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "AddManagerClassTag", ex.ToString());
            }

            return isadd;
        }

        public void GetManagerTagAppointList(int tid, out List<UTag> outlist, out List<UTag> inlist)
        {
            outlist = new List<UTag>();
            inlist = new List<UTag>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var tlist = _dc.UTag.Where(ta=> ta.TAGtype==(int)Config.TagType.CLASS && ta.TAGlevel==1);
                    
                    foreach(var t in tlist)
                    {
                        if(_dc.UTagRelation.Any(r=>r.TRclasstagid==t.TAGid&&r.TRfreetagid==tid))
                            inlist.Add(t);
                        else
                            outlist.Add(t);
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetManagerTagAppointList", ex.ToString());
            }
        }

        public bool SetManagerTagAppointIn(int tid, string[] tidlist)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    List<UTagRelation> utmlist = new List<UTagRelation>();
                    foreach (string tagid in tidlist)
                    {
                        int t = int.Parse(tagid);
                        var mt = _dc.UTagRelation.SingleOrDefault(tm => tm.TRfreetagid==tid && tm.TRclasstagid==t);
                        if (mt == null)
                        {
                            UTagRelation utm = new UTagRelation();
                            utm.TRclasstagid = t;
                            utm.TRfreetagid = tid;
                            utmlist.Add(utm);
                        }
                    }
                    _dc.UTagRelation.InsertAllOnSubmit(utmlist);
                    _dc.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {

                _log.RecordExceptionFormat(_log.DBExceptionPath, "SetManagerTagAppointIn", ex.ToString());
            }
            return false;
        }

        public bool SetManagerTagAppointOut(int tid, string[] tidlist)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var mt = _dc.UTagRelation.Where(tm => tm.TRfreetagid == tid && tidlist.Contains(tm.TRclasstagid.ToString()));
                    if (mt != null)
                    {
                        _dc.UTagRelation.DeleteAllOnSubmit(mt);
                    }

                    _dc.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "SetManagerTagAppointOut", ex.ToString());
            }
            return false;
        }





       
    }
}
